package com.polaris.live.adapter

import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.polaris.live.R
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.DateUtils
import com.polaris.live.databinding.AdapterRoomBackgroundDiyBinding
import com.polaris.live.resp.back_resp.UserPartyBackgroundVo
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.loadImage

/**
 * 自定义房间背景适配器
 *
 * @author Created by 半仙 on 2024/12/18/018 20:19
 */
class RoomBackgroundDiyAdapter(
    private val mIsPopup: Boolean,
) : BaseBindingAdapter<AdapterRoomBackgroundDiyBinding, UserPartyBackgroundVo>() {

    init {
        addChildClickViewIds(R.id.iv_user_background, R.id.tv_delete, R.id.tv_renewal)
    }

    //mSelectPosition这个参数只有在背包页的时候才会用到    如果是半弹窗是用不到的
    private var mSelectPosition = 0

    override fun convert(holder: VBViewHolder<AdapterRoomBackgroundDiyBinding>, item: UserPartyBackgroundVo) {
        holder.vb.apply {
            tvCoverState.visibility = View.GONE
            ivCoverCenterTips.visibility = View.GONE
            tvVipTips.visibility = View.GONE
            tvDelete.visibility = View.GONE
            ivRemove.visibility = View.GONE
            ivMask.visibility = View.GONE
            //已激活
            if (item.activated) {
                item.auditStatus?.let { state ->
                    if (state == AppConst.CoverPhoto.UNDER_REVIEW) {
                        tvCoverState.visibility = View.VISIBLE
                        tvCoverState.text = StringUtils.getString(string.under_review_cover)
                    } else {
                        if (!item.background.isNullOrEmpty()) {
                            tvDelete.visibility = View.VISIBLE
                        }
                    }
                }
            }

            //判断有无封面照
            if (item.background.isNullOrEmpty() || item.auditStatus == AppConst.CoverPhoto.REVIEW_NOT) {
                setGrayCenter(holder, item)
            } else {
                Glide.with(context).loadImage(item.background).transform(RoundedCorners(4.autoDp))
                    .into(ivUserBackground)
                if (!item.activated) {
                    ivCoverCenterTips.setImageResource(R.drawable.room_background_lock)
                    ivCoverCenterTips.visibility = View.VISIBLE
                    ivMask.visibility = View.VISIBLE
                }
            }

            //背景框选中状态逻辑
            backgroundSelect(holder, item)
            //时间逻辑
            setTime(holder, item)
            //底部按钮展示逻辑
            bottomButton(holder, item)
        }
    }

    //背景选中状态逻辑
    private fun backgroundSelect(holder: VBViewHolder<AdapterRoomBackgroundDiyBinding>, item: UserPartyBackgroundVo) {
        holder.vb.apply {
            //背景框   //弹窗的时候以是否使用为准
            if (item.activated) {
                if (item.useStatus.toBoolean()) {
                    tvUsering.visibility = View.VISIBLE
                } else {
                    tvUsering.visibility = View.GONE
                }
                if (mIsPopup) {
                    if (item.useStatus.toBoolean()) {
                        consTitle.setBackgroundResource(R.drawable.back_pack_select_back)
                    } else {
                        consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
                    }
                } else {
                    if (mSelectPosition == holder.absoluteAdapterPosition) {
                        consTitle.setBackgroundResource(R.drawable.back_pack_select_back)
                    } else {
                        consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
                    }
                }
            } else {
                tvUsering.visibility = View.GONE
                consTitle.setBackgroundResource(R.drawable.r8_back_ffffff)
            }
        }
    }

    //底部按钮逻辑
    private fun bottomButton(holder: VBViewHolder<AdapterRoomBackgroundDiyBinding>, item: UserPartyBackgroundVo) {
        holder.vb.apply {
            //已激活槽位
            if (item.activated) {
                //是不是弹窗   如果不是弹窗  底部按钮就是 佩戴和卸下
                if (!mIsPopup) {
                    if (item.useStatus.toBoolean()) {
                        tvRenewal.text = StringUtils.getString(string.remove)
                        ivRemove.visibility = View.VISIBLE
                        tvRenewal.setTextColor(ColorUtils.getColor(R.color.color_43c158))
                        tvRenewal.setBackgroundResource(0)
                    } else {
                        tvRenewal.text = StringUtils.getString(string.wear)
                        tvRenewal.setTextColor(ColorUtils.getColor(R.color.black))
                        tvRenewal.setBackgroundResource(com.polaris.live.common.R.drawable.r26_radient)
                    }
                } else {
                    if (item.expireTime == null) { //是永久的
                        tvRenewal.text = StringUtils.getString(string.permanent)
                    } else {
                        tvRenewal.text = StringUtils.getString(string.renewal)
                    }
                    tvRenewal.setTextColor(ColorUtils.getColor(R.color.color_2e2e2e))
                    tvRenewal.setBackgroundResource(R.drawable.r26_fff0f0f0)
                }
            } else {
                tvRenewal.text = StringUtils.getString(string.activate)
                if (item.positionType == CommonConst.BackgroundType.FAMILY || item.positionType == CommonConst.BackgroundType.BUY) {
                    tvRenewal.setTextColor(ColorUtils.getColor(R.color.white))
                    tvRenewal.setBackgroundResource(R.drawable.background_diy_lock)
                } else {
                    tvVipTips.visibility = View.VISIBLE
                    tvRenewal.setTextColor(ColorUtils.getColor(R.color.color_ff793800))
                    tvRenewal.setBackgroundResource(R.drawable.background_diy_vip_lock)
                }
            }
        }
    }

    //设置灰色中心
    private fun setGrayCenter(
        binding: VBViewHolder<AdapterRoomBackgroundDiyBinding>,
        item: UserPartyBackgroundVo,
    ) {
        binding.vb.apply {
            ivUserBackground.setImageResource(R.drawable.r4_back_fff0f0f0)
            ivCoverCenterTips.visibility = View.VISIBLE
            if (item.activated) {
                ivCoverCenterTips.setImageResource(R.drawable.room_background_add)
            } else {
                ivCoverCenterTips.setImageResource(R.drawable.room_background_lock)
            }
        }
    }

    //时间逻辑
    private fun setTime(holder: VBViewHolder<AdapterRoomBackgroundDiyBinding>, item: UserPartyBackgroundVo) {
        holder.vb.apply {
            if (item.activated) {
                val expiredTime = item.expireTime
                if (expiredTime == null) {
                    tvTime.text = StringUtils.getString(string.permanent)
                } else {
                    tvTime.text = DateUtils.formatTime(expiredTime.toLong() - CacheUtil.getServerTime())
                }
            } else {
                tvTime.text = "0"
            }
        }
    }

    fun setSelectPosition(position: Int) {
        mSelectPosition = position
        notifyDataSetChanged()
    }

    fun getSelectData() = data.getOrNull(mSelectPosition)

    fun getSelectPosition() = mSelectPosition
}